
package com.hexinfo.dmpro.sparing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hexinfo.dmpro.common.utils.CommonConstants;
import com.hexinfo.dmpro.common.utils.ScanCommonConstants;
import com.hexinfo.dmpro.common.utils.StringUtils;
import com.hexinfo.dmpro.sparing.dto.BackupLibraryDTO;
import com.hexinfo.dmpro.sparing.dto.TotalDTO;
import com.hexinfo.dmpro.sparing.model.SyncCompleteLibraryTask;
import com.hexinfo.dmpro.sparing.dao.SyncCompleteLibraryTaskMapper;
import com.hexinfo.dmpro.sparing.service.SyncCompleteLibraryTaskService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 热备全库日志任务表
 *
 * @author yemw
 * @date 2023-11-30 11:06:35
 */
@Service
public class SyncCompleteLibraryTaskServiceImpl extends ServiceImpl<SyncCompleteLibraryTaskMapper, SyncCompleteLibraryTask> implements SyncCompleteLibraryTaskService {

    @Override
    public Page queryList(Page page, SyncCompleteLibraryTask syncCompleteLibraryTask) {
        LambdaQueryWrapper<SyncCompleteLibraryTask> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotBlank(syncCompleteLibraryTask.getTaskName()),SyncCompleteLibraryTask::getTaskName, syncCompleteLibraryTask.getTaskName())
                .like(StrUtil.isNotBlank(syncCompleteLibraryTask.getTableNameList()),SyncCompleteLibraryTask::getTableNameList,StringUtils.toLowerCase(syncCompleteLibraryTask.getTableNameList()))
                .like(StrUtil.isNotBlank(syncCompleteLibraryTask.getDatabaseName()),SyncCompleteLibraryTask::getDatabaseName,StringUtils.toLowerCase(syncCompleteLibraryTask.getDatabaseName()))
                .eq(StrUtil.isNotBlank(syncCompleteLibraryTask.getTaskState()),SyncCompleteLibraryTask::getTaskState,syncCompleteLibraryTask.getTaskState())
                .apply(StrUtil.isNotBlank(syncCompleteLibraryTask.getStaDate()),"CREATE_TIME between to_date('" + syncCompleteLibraryTask.getStaDate() + "','yyyy-MM-dd HH24:MI:SS') and to_date('"+ syncCompleteLibraryTask.getEndDate() + "','yyyy-MM-dd HH24:MI:SS')")
                .eq(SyncCompleteLibraryTask::getDel,CommonConstants.STATUS_NORMAL).orderByDesc(SyncCompleteLibraryTask::getCreateTime);
        Page returnPage = this.baseMapper.selectPage(page, wrapper);
        return returnPage;
    }

    @Override
    public TotalDTO hotBackupLibraryTotal(BackupLibraryDTO backupLibraryDTO) {
        backupLibraryDTO.setStaTime(backupLibraryDTO.getStaTime()+" 00:00:00");
        backupLibraryDTO.setEndTime(backupLibraryDTO.getEndTime()+" 23:59:59");
        backupLibraryDTO.setYwc(ScanCommonConstants.ClusterName.YWC.value);
        backupLibraryDTO.setDzx(ScanCommonConstants.ClusterName.DDZ.value);
        backupLibraryDTO.setJxz(ScanCommonConstants.ClusterName.JXZ.value);
        backupLibraryDTO.setYc(ScanCommonConstants.ClusterName.SB.value);
        return this.baseMapper.hotBackupLibraryTotal(backupLibraryDTO);
    }

    @Override
    public TotalDTO hotBackupLibraryTableTotal(BackupLibraryDTO backupLibraryDTO) {
        backupLibraryDTO.setStaTime(backupLibraryDTO.getStaTime()+" 00:00:00");
        backupLibraryDTO.setEndTime(backupLibraryDTO.getEndTime()+" 23:59:59");
        backupLibraryDTO.setYwc(ScanCommonConstants.ClusterName.YWC.value);
        backupLibraryDTO.setDzx(ScanCommonConstants.ClusterName.DDZ.value);
        backupLibraryDTO.setJxz(ScanCommonConstants.ClusterName.JXZ.value);
        backupLibraryDTO.setYc(ScanCommonConstants.ClusterName.SB.value);
        return this.baseMapper.hotBackupLibraryTableTotal(backupLibraryDTO);
    }

    @Override
    public Date maxCreateTime(String dbName) {
        return this.baseMapper.maxCreateTime(dbName);
    }

    @Override
    public List<SyncCompleteLibraryTask> dbHotBackupState() {
        return this.baseMapper.dbHotBackupState();
    }
}
